package utils

import (
	"fmt"
	"github.com/bwmarrin/snowflake"
	"os"
	"path/filepath"
	"sync"
)

var once sync.Once

type SnowflakeStruct struct {
	node *snowflake.Node
}

var SnowflakeInstance *SnowflakeStruct

func GetSnowflakeInstance() *SnowflakeStruct {
	if SnowflakeInstance == nil {
		once.Do(func() {
			node, _ := snowflake.NewNode(1)
			SnowflakeInstance = &SnowflakeStruct{
				node: node,
			}
		})
	}
	return SnowflakeInstance
}

func (n *SnowflakeStruct) Generate() int64 {
	return n.node.Generate().Int64()

}

func GetFilePath(fileName string) string {
	path, _ := os.Getwd()
	id := GetSnowflakeInstance().Generate()
	fileContainer := filepath.Join(path, "fileContainer", fmt.Sprintf("%v", id))
	_, err := os.Stat(fileContainer)
	if os.IsNotExist(err) {
		os.MkdirAll(fileContainer, os.ModeDir)
	}
	return filepath.Join(fileContainer, fileName)
}

func GetFileId() string {
	return fmt.Sprintf("%v", GetSnowflakeInstance().Generate())
}
